package itesm.coler.data;
import itesm.coler.data.UserDAO;
import itesm.coler.data.MessageTypeDAO;

import java.sql.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
//import javax.sql.DataSource;
//import javax.sql.PooledConnection;

public class MySQLDAOFactory extends DAOFactory {
	// MySQL concrete DAO Factory implementation
	
	 private static final String PROPERTY_URL = "jdbc/waveColer";
//    DataSource ds;	
//	  public static final String DRIVER="com.mysql.jdbc.Driver";
//	  public static  String host = "wavecoler.lag.itesm.mx";
//	  public static final String DBURL="jdbc:mysql://"+host+":3306/wavecoler";
	
	  // method to create MySQL connections
	  public static Connection createConnection() {		   
	    // Use DRIVER and DBURL to create a connection
	    // Recommend connection pool implementation/usage
		Connection connection = null;
		try {
		  	  //Using direct connection
	/*			Class.forName(DRIVER);
				String user = "coler";
				String password = "wavecoler";
				con = DriverManager.getConnection(DBURL, user, password);
	*/      
				//Using a connection pool
		    	InitialContext ctx = new InitialContext();
		        DataSource ds = (DataSource) ctx.lookup(PROPERTY_URL);
      //          PooledConnection pconnection = ds.getPooledConnection();
                connection = ds.getConnection();
		    	    
		    }  catch (NamingException e) {
		    	 throw new DAOConfigurationException(
		                    "DataSource '" + PROPERTY_URL + "' is missing in JNDI.", e);
		    } catch (SQLException sqle){
		    	 sqle.printStackTrace();
		     } catch (Exception e){
		    	 e.printStackTrace();
		     }	 
		  return connection;
	  }
	  
	  public UserDAO getUserDAO(){
	    // MySQLUserDAO implements UserDAO
	    return (UserDAO) new MySQLUserDAO();
	  }
	  
	  public CourseDAO getCourseDAO(){
		  return (CourseDAO) new MYSQLCourseDAO();
	  }

	@Override
	public ProblemDAO getProblemDAO() {
		// TODO Auto-generated method stub
		return (ProblemDAO) new MySQLProblemDAO();
	}
	  
	  public TeamDAO getTeamDAO(){
		  return (TeamDAO) new MYSQLTeamDAO();
	  }

	@Override
	public WordDAO getWordDAO() {
		// TODO Auto-generated method stub
		return (WordDAO) new MySQLWordDAO();
	}

	
	public SessionDAO getSessionDAO() {
		// TODO Auto-generated method stub
		return (SessionDAO) new MySQLSessionDAO();
	}

	@Override
	public MessageTypeDAO getMessageTypeDAO() {
		// TODO Auto-generated method stub
		return (MessageTypeDAO) new MySQLMessageTypeDAO();
	}

	@Override
	public MessageCategoryDAO getMessageCategoryDAO() {
		// TODO Auto-generated method stub
		return (MessageCategoryDAO) new MySQLMessageCategoryDAO();
	}
	
	@Override
	public MessageDAO getMessageDAO() {
		// TODO Auto-generated method stub
		return (MessageDAO) new MySQLMessageDAO();
	}
	  
}

